<?php

namespace app\merchant\controller;

use app\common\model\Merchant as MerchantModel;
use app\common\model\Goods;
use app\common\model\Store as StoreModel;
use app\common\model\GoodsCategory as GoodsCategory;
use think\Db;

class Merchant extends Common
{
    public function update2()
    {
        $store_id = (int)input('param.store_id');
        $store_face_img = input('param.store_face_img');
        $store_bg_img = input('param.store_bg_img');
        $store_introduce = input('param.store_introduce');
        $cs_phone = input('param.cs_phone', '');
        $res = Db::name('store')->where('store_id', $store_id)->update([
            'store_face_img' => $store_face_img,
            'store_bg_img' => $store_bg_img,
            'store_introduce' => $store_introduce,
            'store_cs_phone' => $cs_phone
        ]);
       return returnJson(1, '修改成功');

    }

    public function upload_main_img()
    {
        // 店铺编号+商户编号+时间戳+随机4位数
        $imgRootPath = config('imgRootPath');
        $filename = Goods::makeFileName();
        // halt($filename);
        $whereName = 'store';
        $type = Goods::upload($whereName, $filename);
        if ($type) {
            $data = [
                'access_url' => $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type,
                'filename' => $filename . '.' . $type,
            ];
            return returnJson(1, '上传成功', $data);
        }
    }

    public function store_face_img()
    {
        // 店铺编号+商户编号+时间戳+随机4位数
        $imgRootPath = config('imgRootPath');
        $filename = Goods::makeFileName();
        // halt($filename);
        $whereName = 'store';
        $type = Goods::upload($whereName, $filename);
        if ($type) {
            $data = [
                'access_url' => $imgRootPath . '/' . $whereName . '/' . $filename . '.' . $type,
                'filename' => $filename . '.' . $type,
            ];
            return returnJson(1, '上传成功', $data);
        }
    }

    public function modify()
    {
        $phone = session('merchant.merchant_tel');
        $data = MerchantModel::getMerchant($phone);
        $merchantId = session('merchant.merchant_id');
        $store = StoreModel::getStoreByMerchantId($merchantId);
        return view('modify', ['data' => $data, 'store' => $store]);
    }

    public function update()
    {
        $store_id = input('param.store_id');
        $store = StoreModel::find($store_id);
        // 主分类
        $mainCategory = GoodsCategory::getMainCategory();
        $this->assign(['store' => $store, 'mainCategory' => $mainCategory]);
        if (request()->isPost()) {
            $data = request()->except('__token__');
            $model = new StoreModel;
            $validate = $model->intValidate();
            if (!($validate->scene('update')->check($data)))
                return returnJson(0, $validate->getError());
            if ($store->isUpdate(true)->save($data) !== false) {
                return returnJson(1, '修改成功');
            } else {
                return returnJson(0, '修改失败，请稍后重试！');
            }
        }
        return view('update');
    }

    public function activeDo()
    {
        if (request()->isAjax()) {
            MerchantModel::activeMerchantAccount();
            return returnJson(1, '激活成功', '');
        } else {
            return returnJson(0, '请使用正确的方式提交数据');
        }
    }
}
